using System.IO;
using UnityEngine;

/// <summary>
/// 自动创建脚本
/// </summary>
public class CreateScript : UnityEditor.AssetModificationProcessor
{
    static string uiMgrTemplatePath = Application.dataPath + "/Scripts/Editor/FastScript/UIMgrTemplate.txt";
    static string modulelTempletePath = Application.dataPath + "/Scripts/Editor/FastScript/ModuleTemplate.txt";
    static string modelTempletePath = Application.dataPath + "/Scripts/Editor/FastScript/ModelTemplate.txt";


    private static string GetScriptName(string path)
    {
        string[] pathNames = path.Split('/');
        string scriptAllPath = pathNames[pathNames.Length - 1];
        string[] names = scriptAllPath.Split('.');
        return names[0];
    }

    private static string CreateModuleScript(string scriptName)
    {
        string str = File.ReadAllText(modulelTempletePath);
        str = str.Replace("#MODULENAME#", scriptName);
        return str;
    }

    private static string CreateUIMgrScript(string scriptName)
    {
        string str = File.ReadAllText(uiMgrTemplatePath);
        return str;
    }

    private static string CreateModelScript(string scriptName)
    {
        string str = File.ReadAllText(modelTempletePath);
        str = str.Replace("#MODELNAME#", scriptName);
        return str;
    }

    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        if (path.EndsWith(".cs") && path.Contains("Assets/Scripts"))
        {
            string allText = string.Empty;
            string scriptName = GetScriptName(path);
            if (scriptName.Equals("UIMgr"))
            {
                allText = CreateUIMgrScript(scriptName);
                File.WriteAllText(path, allText);
            }

            if (path.IndexOf("Module") != -1 && scriptName.IndexOf("Module") != -1)
            {
                allText = CreateModuleScript(scriptName);
                File.WriteAllText(path, allText);
            }

            if (path.IndexOf("Model") != -1 && scriptName.IndexOf("Model") != -1)
            {
                allText = CreateModelScript(scriptName);
                File.WriteAllText(path, allText);
            }
        }
    }
}